package library.push;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

/**
 * 推送处理工具类
 *
 * @author Created by qiang.hou on 2018/4/17.
 * @version 1.0
 */
public class NotificationUtils {

    public static final String PUSH_DATA_PARAM = "push_data_param";

    private static int notify_request_code = 1;

    private static Class dispatchActivity;

    private static int smallIcon = R.mipmap.ic_launcher;

    public static void init(Class pushDispatchActivity, int notificationSmallIcon) {
        dispatchActivity = pushDispatchActivity;
        smallIcon = notificationSmallIcon;
    }

    /**
     * 处理推送数据
     *
     * @param mContext  Context
     * @param isMessage 是否是自定义消息
     * @param data      bundle
     */
    public static void dealPushMessage(Context mContext, boolean isMessage, Bundle data) {
        if (null == data) {
            return;
        }
        PushData pushData = new PushData(isMessage, data);

        // 判断推送信息是否完整
        /*
         * for 2.4
         */
        if (pushData.getDataString() == null && pushData.getDataString().equals("")) {
            return;
        }

        Intent targetIntent = new Intent();
        targetIntent.putExtra(PUSH_DATA_PARAM, pushData);
        if (null != dispatchActivity) {
            targetIntent.setClass(mContext, dispatchActivity);
        }

        if (isMessage) {
            addNotification(mContext,
                    pushData.getTitle(), pushData.getMessage(), targetIntent);
        } else {
            startActivity(mContext, targetIntent);
        }

    }

    /**
     * 添加通知
     */
    private static void addNotification(Context context, String title,
                                        String message, Intent targetIntent) {
        notify_request_code++;
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("1", "channelName", NotificationManager.IMPORTANCE_HIGH);
            if (mNotificationManager != null) {
                mNotificationManager.createNotificationChannel(channel);
            }
        }

        PendingIntent mainPendingIntent = PendingIntent.getActivity(
                context, notify_request_code, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        // 创建 Notification.Builder 对象
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "")
                .setSmallIcon(smallIcon)
                //点击通知后自动清除
                .setAutoCancel(true)
                .setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.alarm))
                .setContentTitle(title)
                .setContentText(message)
                .setShowWhen(true)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(mainPendingIntent);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            builder.setChannelId("1");
        }

        //发送通知
        if (mNotificationManager != null) {
            mNotificationManager.notify(notify_request_code, builder.build());
        }
    }

    private static void startActivity(Context context, Intent targetIntent) {
        targetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(targetIntent);
    }
}
